//
//  SHPKeyboardCell.swift
//  weibo
//
//  Created by songhuipan on 16/4/25.
//  Copyright © 2016年 songhuipan. All rights reserved.
//

import UIKit

class SHPKeyboardCell: UICollectionViewCell {
    
    var emoticonModels:[SHPEmoticonModel]?{
        didSet{
            
            for button in buttons{
                button.hidden = true
            }
            
            for (i,emoticonModel) in emoticonModels!.enumerate(){
                
                let button = buttons[i]
                button.hidden = false
                // 给按钮的属性赋值，点击按钮将按钮对应的属性传递出去
                button.emoticonModel = emoticonModel
                let path = "\(emoticonModel.path ?? "")\(emoticonModel.png ?? "")"
                
                if emoticonModel.isEmoji{
                    
                    button.setTitle(((emoticonModel.code ?? "") as NSString).emoji(), forState: UIControlState.Normal)
                    button.setImage(nil, forState: UIControlState.Normal)
                    
                }else {
                    
                    let image = UIImage(named: path, inBundle: SHPEmoticonTool.sharedTools.emoticonBundle, compatibleWithTraitCollection: nil)
                    button.setImage(image, forState: UIControlState.Normal)
                    button.setTitle(nil, forState: UIControlState.Normal)
                }
            }
        }
    }
    // MARK:存贮按钮数组
    var buttons:[SHPEmoticonButton] = [SHPEmoticonButton]()
    // MARK:懒加载删除按钮
    private lazy var deleteButton:UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension SHPKeyboardCell {
    // MARK:点击键盘图片按钮调用的方法
    func buttonClick(sender:SHPEmoticonButton){
        // 发送通知，将被点击按钮所对应的模型属性传递出去
        NSNotificationCenter.defaultCenter().postNotificationName("buttonClickNotification", object: sender.emoticonModel)
    }
    // MARK:点击删除按钮调用的方法
    func deleteButtonClick(){
        NSNotificationCenter.defaultCenter().postNotificationName("deleteButtonClickNotification", object: nil)
    }
    // MARK:设置主界面UI
    func setupUI(){
        self.backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addChildrenButton()
        contentView.addSubview(deleteButton)
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()

        let w = (self.frame.width - 10) / CGFloat(emoticonMaxCol)
        let h = (self.frame.height - 20) / CGFloat(emoticonMaxRow)
        
        for (i,button) in buttons.enumerate(){
            let x = CGFloat((i % emoticonMaxCol)) * w
            let y = CGFloat((i / emoticonMaxCol)) * h
            
            button.frame = CGRectMake(x+5, y, w, h)
            
        }
        deleteButton.frame = CGRectMake(self.frame.width-w-5, h*2, w, h)
    }
    // MARK:创建按钮并添加
    func addChildrenButton(){
        for _ in 0..<emoticonMaxCount{
            let button = SHPEmoticonButton()
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
            contentView.addSubview(button)
            buttons.append(button)
        }
    }
}























































